<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-倒计时折扣
// 代码日期：v3.0 2018-7-20
// 作者：杨道泉
// 文件名：VipResourceController.class.php
// 功能： 读取和操作VIP用户资源

class VipResourceController extends QnuiBaseController {
    public function index(){
        $this->display();
    }
    public function get_resource_list(){
              $RSC = M("VipResource");
              if(session("VISITOR_NICK") != "kekydream"){
                  $where = Array("sid"=>session("SHOP_ID"));
                   }
              $res =  $RSC->where($where)
              ->order("id desc")
              ->getField("`id`,`sid`,`nick`,`name`,`type`,`description`,`start_time`,`end_time`,`status`,`item_code`,`counting`,`quantity`,`unit_price`");
                  
        $re = new \StdClass();
        $re->code=200;
        if(!$res){
            $re->data=Array();
        }else{
            foreach($res as $k=>$r){
                $res[$k]["unit_price"] = $res[$k]["unit_price"]/100;
                }
           $re->data=array_values($res);
           }
       $this->ajaxReturn($re);
    }
    public function add_resource(){
        $id_save = I("id");
        $data["sid"] = I("sid");
        $data["name"] = I("name");
        $data["type"] = I("type");
        $data["description"] = I("description");
        $data["start_time"] = I("start_time");
        $data["end_time"] = I("end_time");
        $data["status"] = I("status");
        $data["item_code"] = I("item_code");
        $data["unit_price"] = I("unit_price")*100;
        $data["quantity"] = I("quantity");
        $data["counting"] = I("counting");
        $data["nick"] = I("nick");
        
        if(session("VISITOR_NICK") == "kekydream" || session("VISITOR_NICK") == "zlp130"){
            $RSC = M("VipResource");
            if($id_save){
                $id = $RSC->where(Array('id'=>$id_save))->save($data);
            }else{
                $id = $RSC->add($data);
                 }
           }
        if($id){
               $re = new \StdClass();
               $re->code=200;
               $re->data=$id;
        }else{
            $re = new \StdClass();
            $re->code=53;
            $re->msg="";
         }
        $this->ajaxReturn($re);
    }
    public function remove_resource(){
        $id = I("id");
        if(session("VISITOR_NICK") == "kekydream"){
            $RSC = M("VipResource");
            $re = $RSC->delete($id);
          }
        if($re){
            $re = new \StdClass();
            $re->code=200;
        }else{
            $re = new \StdClass();
            $re->code=53;
          }
        $this->ajaxReturn($re);
    }
    public function get_pay_url(){
        $item_code = I("item_code");
        $cyc_unit = I("cyc_unit");
        $cyc_num = I("cyc_num");
        $quantity = I("quantity");
        $resource_id=I("id");
        $fee = I("fee");
        $RSC = M("VipResource");
        $resource = current($RSC->where(Array("id"=>$resource_id))->getField("id,sid,nick,unit_price"));
        if($fee>0){
            $unit_price = $fee*100;
        }else{
            $unit_price = $resource["unit_price"];
        }
        $ORD = M("VipOrders");
        $out_trade_code = $ORD->add(Array(
                            'sid'=>$resource["sid"]?$resource["sid"]:session("SHOP_ID"),
                            'cyc_unit'=>$cyc_unit,
                            'cyc_num'=>$cyc_num,
                            'quantity'=>$quantity,
                            'nick'=>$resource["nick"]?$resource["nick"]:session("VISITOR_NICK"),
                            'time'=>date("Y-m-d H:i:s"),
                            'item_code'=>$item_code,
                            'resource_id'=>$resource_id,
                            'fee'=>$unit_price*$quantity
                                ));
        Vendor('taobaosdk/top/request/FuwuPurchaseOrderConfirmRequest');
        $req = new \FuwuPurchaseOrderConfirmRequest;
        $param_order_confirm_query_d_t_o = new \StdClass();
        $param_order_confirm_query_d_t_o->app_key=C("APPKEY");
        $param_order_confirm_query_d_t_o->item_code=$item_code;
        $param_order_confirm_query_d_t_o->cyc_unit=$cyc_unit;
        $param_order_confirm_query_d_t_o->cyc_num=$cyc_num;
        $param_order_confirm_query_d_t_o->out_trade_code=$out_trade_code;
        $param_order_confirm_query_d_t_o->device_type="pc";
        $param_order_confirm_query_d_t_o->quantity=$quantity;
        $req->setParamOrderConfirmQueryDTO(json_encode($param_order_confirm_query_d_t_o));
        $resp = pre_resp($this->c->execute($req));
        $this->ajaxReturn($resp);
    }
    public function get_orders(){
        $shop_id = I("sid");
        $page = I("page");
        $page_size = I("page_size");
        $show_confirmed = I("show_confirmed");
        $ORD = M("VipOrders");
        $where = Array();
        if($shop_id){
            $where = array_merge($where,Array("sid"=>$shop_id));
           }
        if($show_confirmed=="true"){
            $where = array_merge($where,Array("confirm_id"=>array('exp',' IS NOT NULL ')));
          }
        $count = $ORD->where($where)->count();
        $orders = $ORD->where($where)
                      ->limit($page_size*($page-1),$page_size)
                      ->order("id desc")
                      ->getField("id,sid,cyc_unit,cyc_num,quantity,resource_id,confirm_id,fee,item_code,time,nick");
        if(count($orders)==0){
            $orders = Array();
        }else{
            $orders = array_values($orders);
          }
      $re = new \StdClass();
      $re->code=200;
      $re->total = $count;
      $re->orders = $orders;
      $this->ajaxReturn($re);
    }
    public function confirm_order(){
        $fee = I("fee");
        $nick = I("nick");
        $order_id = I("order_id");
        $out_order_id = I("out_order_id");
        $out_confirm_id = I("out_confirm_id");
        Vendor('taobaosdk/top/request/FuwuSpConfirmApplyRequest');
        $req = new \FuwuSpConfirmApplyRequest;
        $param_income_confirm_d_t_o = new \StdClass();
        $param_income_confirm_d_t_o->fee=$fee;
        $param_income_confirm_d_t_o->nick=$nick;
        $param_income_confirm_d_t_o->extend="手动确认";
        $param_income_confirm_d_t_o->appkey=C("APPKEY");
        $param_income_confirm_d_t_o->order_id=$order_id;
        $param_income_confirm_d_t_o->out_order_id=$out_order_id;
        $param_income_confirm_d_t_o->out_confirm_id=$out_confirm_id;
        $req->setParamIncomeConfirmDTO(json_encode($param_income_confirm_d_t_o));
        $resp = pre_resp($this->c->execute($req,session("TOP_SESSION")));
        $this->ajaxReturn($resp);
    }
    public function confirm_order_apply(){
        $id = I("id");
        $order_id = I("order_id");
        $confirm_id = I("confirm_id");
        try{
            if(session("VISITOR_NICK") == "kekydream"){
                $ORD = M("VipOrders");
                $re = $ORD->where(Array("id"=>$id))->save(Array("confirm_id"=>$confirm_id,"order_id"=>$order_id));
                }
        }catch(\Exception $e){
            $msg = $e->getMessage();
         }
        if(!$msg){
            $resp["is_success"]=true;
            $resp["code"]=200;
        }else{
            $resp["code"]=910;
            $resp["msg"]=$msg;
          }
        $this->ajaxReturn($resp);
    }
}